/*
 * Copyright 2014 NAVER Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ideal.ete.collector.service.Impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.AnnotationUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.shaded.org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.ideal.ete.collector.application.AnnotationKey;
import com.ideal.ete.collector.calltree.span.CallTree;
import com.ideal.ete.collector.calltree.span.CallTreeIterator;
import com.ideal.ete.collector.calltree.span.SpanAlign;
import com.ideal.ete.collector.calltree.span.SpanAligner2;
import com.ideal.ete.collector.dao.TraceDao;
import com.ideal.ete.collector.server.bo.AnnotationBo;
import com.ideal.ete.collector.server.bo.MethodTypeEnum;
import com.ideal.ete.collector.server.bo.SpanBo;
import com.ideal.ete.collector.service.SpanResult;
import com.ideal.ete.collector.service.SpanService;
import com.ideal.ete.collector.utils.DefaultSqlParser;
import com.ideal.ete.collector.utils.OutputParameterParser;
import com.ideal.ete.collector.utils.SqlParser;
import com.ideal.ete.collector.utils.TransactionId;

/**
 * @author emeroad
 * @author jaehong.kim
 * @author minwoo.jung
 */
@Service
public class SpanServiceImpl implements SpanService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    @Qualifier("hbaseTraceDaoFactory")
    private TraceDao traceDao;


    private final SqlParser sqlParser = new DefaultSqlParser();
    private final OutputParameterParser outputParameterParser = new OutputParameterParser();


    @Override
    public SpanResult selectSpan(TransactionId transactionId, long selectedSpanHint) {
        if (transactionId == null) {
            throw new NullPointerException("transactionId must not be null");
        }

        final List<SpanBo> spans = traceDao.selectSpan(transactionId);
        if (CollectionUtils.isEmpty(spans)) {
            return new SpanResult(SpanAligner2.FAIL_MATCH, new CallTreeIterator(null));
        }

        final SpanResult result = order(spans, selectedSpanHint);
        final CallTreeIterator callTreeIterator = result.getCallTree();
        final List<SpanAlign> values = callTreeIterator.values();
        // TODO need to at least show the row data when root span is not found. 
        return result;
    }



    private void transitionAnnotation(List<SpanAlign> spans, AnnotationReplacementCallback annotationReplacementCallback) {
        for (SpanAlign spanAlign : spans) {
            List<AnnotationBo> annotationBoList = spanAlign.getAnnotationBoList();
            if (annotationBoList == null) {
                annotationBoList = new ArrayList<>();
                spanAlign.setAnnotationBoList(annotationBoList);
            }
            annotationReplacementCallback.replacement(spanAlign, annotationBoList);
        }
    }

   

    public interface AnnotationReplacementCallback {
        void replacement(SpanAlign spanAlign, List<AnnotationBo> annotationBoList);
    }

    private SpanResult order(List<SpanBo> spans, long selectedSpanHint) {
        SpanAligner2 spanAligner = new SpanAligner2(spans, selectedSpanHint);
        final CallTree callTree = spanAligner.sort();

        return new SpanResult(spanAligner.getMatchType(), callTree.iterator());
    }

}

